package com.haiyou.dc.netty.controller.im;

import com.github.pagehelper.PageInfo;
import com.haiyou.common.util.R;
import com.haiyou.common.util.ValidatorUtil;
import com.haiyou.dc.netty.constant.ImChatConstant;
import com.haiyou.dc.netty.entity.ImChat;
import com.haiyou.dc.netty.entity.dto.ImChatDTO;
import com.haiyou.dc.netty.entity.dto.ImChatListDTO;
import com.haiyou.dc.netty.entity.vo.ImChatVO;
import com.haiyou.dc.netty.service.ImChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author root
 * @date 2020-02-29 22:49:56
 */
@RestController
@RequestMapping("/im/chatRecord")
@Slf4j
@Api(tags = "APP聊天控制器")
public class ImChatRecordApi {

    @Autowired
    private ImChatService imChatService;

    @ApiOperation(value = "获取最近联系人聊天记录")
    @GetMapping("/recentUser/{sender}")
    public R recentUser(@PathVariable("sender") String sender) {
        return R.ok(imChatService.selectMessage(sender));
    }

    @ApiOperation(value = "添加聊天记录")
    @PostMapping("/addChatRecord")
    public R addChatRecord(@RequestBody ImChatDTO imChatDTO) {
        ValidatorUtil.validateEntity(imChatDTO);
        log.info("=====>聊天数据={}", imChatDTO);
        imChatService.saveChat(imChatDTO);
        return R.ok();
    }

    @ApiOperation(value = "获取聊天记录")
    @PostMapping("/queryChatList")
    public R queryChatList(@RequestBody ImChatListDTO imChatListDTO) {
        log.info("=====>获取聊天记录,imChatListDTO={}", imChatListDTO);
        PageInfo<ImChatVO> list = imChatService.queryChatList(imChatListDTO);
        return R.ok(list);
    }

    @ApiOperation(value = "签收聊天记录")
    @GetMapping("/signChat/{id}")
    public R signChat(@PathVariable("id") String id) {
        ImChat imChat = new ImChat();
        imChat.setId(id);
        imChat.setSign(ImChatConstant.SignType.YES.getId());
        imChatService.updateByPrimaryKeySelective(imChat);
        return R.ok();
    }


}